{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">While Loop</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For Loop | While Loop\n",
    "--- | --- \n",
    "Iterate through a certain number of values | Will just keep going until condition evaluates to False\n",
    "Can leave loop using break statement | Can leave loop using break statement"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if we want loop to end at some point, we have to make condition False eventually"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "# Everytime through the loop, it checks condition everytime until count is 6 \n",
    "# can also use a break to break out of while loop. \n",
    "count = 0\n",
    "while count <= 5:\n",
    "    print(count)\n",
    "    count = count + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## break statements \n",
    "will completely break you out of a loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while count <= 5:\n",
    "    if count == 2:\n",
    "        break\n",
    "    count += 1\n",
    "    print count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The while True condition makes it impossible to leave the loop unless you run into a break statement\n",
    "<br>\n",
    "if you are stuck in an infinite loop, use control + c on your computer to break out of it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "Found 2\n"
     ]
    }
   ],
   "source": [
    "num = 0\n",
    "while True:\n",
    "    if num == 2:\n",
    "        print('Found 2')\n",
    "        break\n",
    "    num += 1\n",
    "    print num"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reminder: Use the modulo operator (%) which returns the remainder of the division of the number to the left by the number on its right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1 divided by 5 is 0 remainder 1\n",
    "1 % 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5 divided by 5 is 0 remainder 0\n",
    "5 % 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparison Operator | Function\n",
    "--- | --- \n",
    "< | less than\n",
    "<= | less than or equal to\n",
    "> | greater than\n",
    ">= | greater than or equal to\n",
    "== | equal\n",
    "!= | not equal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "while x % 5 != 0:\n",
    "    x += 1\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "range is useful when we know how many times we want to go through a loop\n",
    "<br>\n",
    "start at 0 and go up to but not including 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "candidates = list(range(0, 5))\n",
    "candidates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[2, 3, 4]\n",
      "[3, 4]\n",
      "[4]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "while len(candidates) > 0: \n",
    "    first = candidates[0]\n",
    "    candidates.remove(first)\n",
    "    print(candidates)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.youtube.com/watch?v=l_dIleafLZ8"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
